<template>
  <div class="meal">
    <cu-header :height="80" />
    <div class="meal-container container">
      <div>
        <div class="panel-title">{{ $t('meal.1') }}</div>
        <div class="goods-info">
          <div class="goods-info-l">
            <img :src="domain + showImg" alt="" />
          </div>
          <div class="goods-info-r">
            <div class="goods-row">
              <span class="goods-label">{{ $t('meal.2') }}：</span>
              <span class="goods-name">{{ detail.commodities.name }}</span>
            </div>
            <div class="goods-row">
              <span class="goods-label">{{ $t('meal.3') }}：</span>
              <span class="goods-desc">{{ detail.commodities.mainLimit }}</span>
            </div>
            <div class="goods-row">
              <span class="goods-label">{{ $t('meal.4') }}：</span>
              <span class="goods-desc">{{ detail.commodities.recommendLimit }}</span>
            </div>
            <div class="goods-row">
              <span class="goods-label">{{ $t('meal.5') }}：</span>
              <span class="goods-desc">{{ detail.commodities.topCount }}</span>
            </div>
          </div>
        </div>
      </div>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane :label="$t('meal.6')" name="first">
          <div class="flex mv40">
            <div class="meal-left flex-none">
              <div class="meal-out">
                <div class="meal-title" data-upper="E" :data-desc="$t('meal.9')">{{ $t('meal.7') }}</div>
              </div>
              <div class="meal-desc">
                <p style="margin:30px 0">{{ $t('meal.8') }}</p>
                <ul>
                  <li v-for="item in getFuncs(detail)" :key="item.typeId"><span class="li-dot"></span>{{ item.productName }}</li>
                </ul>
              </div>
            </div>
            <div class="meal-right flex-auto width0">
              <div class="meal-list flex">
                <div class="meal-item" v-for="(m, mIndex) in detail.functions" :key="m.mealId">
                  <div class="meal-out">
                    <img class="meal-img" :src="require(mIndex == 0 ? '@/assets/images/v1.png' : '@/assets/images/v2.png')" alt="" />
                    <div class="meal-name">{{ m.mealName }}</div>
                  </div>
                  <div class="meal-days">{{ m.mealPrice | num2Money }}KZ</div>
                  <ul class="meal-detail" v-for="(d, index) in m.functionDetail" :key="index">
                    <li><span>{{ d.num }}</span><span>{{ d.unit }}</span></li>
                  </ul>
                  <div class="meal-buy">
                    <button type="button" @click="handleBuy(m)">{{ $t('meal.10') }}</button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="Opcional" name="second">
          <div class="flex mv40">
            <div class="meal-left flex-none">
              <div class="meal-out">
                <div class="meal-title" data-upper="E" :data-desc="$t('meal.9')">{{ $t('meal.7') }}</div>
              </div>
              <div class="meal-desc">
                <p style="margin:30px 0">{{ $t('meal.8') }}</p>
                <ul>
                  <li style="height:24px;line-height: 24px;" v-for="item in getFuncs(detail)" :key="item.typeId"><span class="li-dot"></span>{{ item.productName }}</li>
                </ul>
              </div>
            </div>
            <div class="meal-right flex-auto width0">
              <div class="meal-list flex">
                <div class="meal-item">
                  <div class="meal-out">
                     <img class="meal-img" :src="require('@/assets/images/v3.png')" alt="" />
                     <div class="meal-name">{{ detail.selfFunction.mealName }}</div>
                  </div>

                  <div class="meal-days">{{ totalSelf | num2Money }}KZ</div>
                  <ul class="meal-detail" v-for="(d, index) in detail.selfFunction.functionDetail" :key="index">
                    <li style="width:120px"><input class="meal-input" type="text" v-model="d.num" @change="changeNum(d)"/><span>{{ d.unit }}</span></li>
                  </ul>
                  <div class="meal-buy">
                    <button type="button" @click="handleBuySelf()">{{ $t('meal.10') }}</button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <cu-footer style="border-top:70px solid #f2f2f2" />
  </div>
</template>
<script>
import CuHeader from '@/components/header';
import CuFooter from '@/components/footer/index3';
import { Domain } from '@/constants';
import Api from '@/api';
export default {
  name: 'Meal',
  components: { CuHeader, CuFooter },
  data() {
    return {
      domain: Domain,
      id: '',
      detail: { commodities: {}, functions: [], selfFunction: {} },
      showImg: '',
      activeName: 'first',
    };
  },
  created() {
    const { id } = this.$route.query;
    this.id = id;
    this.loadDetail();
  },
  computed: {
    totalSelf() {
      let total = 0;
      if (this.detail.selfFunction.functionDetail) {
        this.detail.selfFunction.functionDetail.forEach((item) => {
          total = parseFloat((parseFloat(total) * 1000 + parseInt(item.num) * parseFloat(item.price) * 1000) / 1000).toFixed(0);
        });
      }
      return total;
    },
  },
  methods: {
    changeNum(val) {
      if (val.num==''){
        val.num=0
      }
    },
    loadDetail() {
      const params = { commodityId: this.id, language: localStorage.getItem('LANGUAGE') || 'pt' };
      Api.goodsMeal(params).then((res) => {
        console.log(res)
        this.detail = res;
        this.detail.commodities.imgs = res.commodities.imgList ? JSON.parse(res.commodities.imgList) : [];
        this.showImg = this.detail.commodities.imgs.length > 0 ? this.detail.commodities.imgs[0] : '';
        console.log(this.showImg)
      });
    },
    handleClick(tab) {
      this.activeName = tab.name;
    },
    getFuncs(detail) {
      if (detail.functions.length > 0) {
        return detail.functions[0].functionDetail;
      } else {
        return [];
      }
    },
    handleBuy(meal) {
      const params = {
        commodityId: this.id,
        isMeal: 1,
        name: meal.mealName,
        mealId: meal.mealId,
        orderDetails: meal.functionDetail,
      };
      Api.createGoodsMealOrder(params).then((res) => {
        console.log(res);
        // this.$message(this.$t('meal.11'));
        this.$router.push({path:"/Pay/"+res.id,query:{price:res.payMoney}})
      });
    },
    handleBuySelf() {
      const fDetail = this.detail.selfFunction.functionDetail.map((item) => {
        return Object.assign(item, { num: parseInt(item.num) });
      });
      const params = {
        commodityId: this.id,
        isMeal: 0,
        name: this.detail.selfFunction.mealName,
        mealId: this.detail.selfFunction.mealId,
        orderDetails: fDetail,
      };
      console.log(params);

      Api.createGoodsMealOrder(params).then((res) => {
        console.log(res);
        // this.$message(this.$t('meal.11'));
        this.$router.push({path:"/Pay/"+res.id,query:{price:res.payMoney}})
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.meal-out{
  height: 170px;
}
.meal-container {
  margin-top: 95px;
  .panel-title {
    font-size: 24px;
    color: #333;
    font-weight: bold;
    margin-bottom: 30px;
  }
  .goods-info {
    display: flex;
    margin-bottom: 30px;
  }
  .goods-info-l {
    flex: none;
    margin-right: 30px;
    img {
      width: 200px;
      height: 200px;
      display: inline-block;
      border-radius: 6px;
      object-fit: cover;

    }
  }
  .goods-info-r {
    flex: auto;
    width: 0;
    .goods-row {
      margin-bottom: 20px;
      background-color: #fb8c24;

      padding: 10px;
      .goods-label {
        color: #595959;
        font-size: 16px;
        display: inline-block;
        width: 30em;
        color: #fff;
      }
      .goods-name {
        font-weight: bold;
        font-size: 18px;
        // color: #333;
        color: #fff;
      }
      .goods-desc {
        // color: #333;
        color: #fff;
      }
    }
  }
}
.meal-left {
  margin-right: 32px;
}
.meal-title {
  position: relative;
  color: #262626;
  font-size: 48px;
  font-family: Source Han Sans CN;
  font-weight: bold;
  letter-spacing: 5px;
  margin-bottom: 100px;
  transform-style: preserve-3d;
  margin-top: 30px;
}
.meal-title::before {
  content: attr(data-upper);
  font-weight: bold;
  font-size: 116px;
  color: #f2f2f2;
  position: absolute;
  left: -25px;
  top: -50px;
  transform: translateZ(-1px);
}
.meal-title::after {
  content: attr(data-desc);
  font-weight: bold;
  font-size: 20px;
  color: #e8e8e8;
  position: absolute;
  left: 0px;
  top: 93px;
  transform: translateZ(-1px);
  letter-spacing: 0;
}
.meal-desc p {
  color: #333;
  font-weight: bold;
  font-size: 16px;
  padding-left: 15px;
  margin-top: 20px;
  margin-bottom: 20px;
}
.meal-desc ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.meal-desc li {
  list-style: none;
  color: #595959;
  font-size: 14px;
  margin-bottom: 24px;
  line-height: 19px;
}
.li-dot {
  width: 4px;
  height: 4px;
  border-radius: 4px;
  background-color: #fb8c24;
  display: inline-block;
  margin-right: 10px;
  vertical-align: middle;
}
.meal-item {
  margin-right: 32px;
  width: calc((100% - 64px) / 3);
  border: 1px solid #e8e8e8;
  box-sizing: border-box;
  padding: 32px 20px;
  height: 565px;
  text-align: center;
}
.meal-item:last-child {
  margin-right: 0;
}
.meal-img {
  width: 56px;
  height: 56px;
}
.meal-name {
  margin-top: 30px;
  color: #333333;
  font-size: 24px;
  font-weight: bold;
}
.meal-days {
  color: #fb8c24;
  font-size: 18px;
  margin: 30px auto;
  display: block;
  text-align: center;

  width: 140px;
}
.meal-detail {
  list-style: none;
  margin: 0;
  padding: 0;
}
.meal-detail li {
  list-style: none;
  // text-align:left;
  // width:30%;
  width: 100px;
  margin: 0 auto;
  padding: 0;
  margin-bottom: 24px;
  display: flex;
  justify-content: center;
  span{
    display: block;
    width: 50%;

  }
  input{
    width: 50%;
  }
}
.meal-detail li .meal-input {
  // width: 60px;
  margin-right: 10px;
  text-align: center;
}
.meal-detail li img {
  display: inline-block;
  width: 16px;
  height: 16px;
}
.meal-buy button {
  // width: 141px;
  height: 40px;
  background: #fb7924;
  border-radius: 4px;
  color: #fff;
  line-height: 38px;
  border: none;
  outline: none;
  padding: 0 10px;
  cursor: pointer;
  &:hover {
    background: #fdaf66;
  }
  &:active {
    background: #e27e20;
  }
}
</style>
